package com.ptszyxx.popose.module.main.mine.vm;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ptszyxx.popose.common.enums.ShopTabEnum;
import com.ptszyxx.popose.common.utils.YBusUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseListResponse;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnErrorResult;
import com.ysg.http.callback.OnSuccessListResult;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.mine.ShopEntity;
import com.ysg.http.data.entity.mine.ShopMyEntity;
import com.ysg.http.data.entity.mine.ShopMyResult;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;

public class MineShopTabVM extends BaseViewModel<CommonRepository> {
    public ShopEntity currentItem;
    public ShopMyEntity currentMyItem;
    public int currentPosition;
    public ShopTabEnum tabEnum;

    public MineShopTabVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onRefreshItemEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onBottomEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onBuyEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onRechargeEvent = new SingleLiveEvent<>();

        public SingleLiveEvent onRefreshMyEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onRefreshItemMyEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onBuyCommand = new BindingCommand(() ->
            uc.onBuyEvent.call()
    );
    //=========================================接口：start===========================================

    /**
     *
     */
    public void requestShopList(boolean refresh) {
        if (refresh) {
            page = 1;
        } else {
            page++;
        }
        Map<String, String> map = new HashMap<>();
        map.put("pageno", String.valueOf(page));
        Observable observable = null;
        switch (tabEnum){
            case avatar:
                observable = model.shopList(map);
                break;
            case mounts:
                map.put("pageno", String.valueOf(page));
                observable = model.shopMountsList(map);
                break;
        }

        HttpUtils.getInstance()
                .list(observable, this,
                        new OnSuccessListResult<ShopEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<ShopEntity> response) {
                                uc.onRefreshEvent.setValue(response.getData());
                                if (refresh && currentItem == null) {
                                    currentItem = response.getData().get(0);
                                    uc.onBottomEvent.call();
                                    push();
                                }
                            }
                        });
    }

    /**
     *
     */
    public void requestShopMy() {
        Map<String, String> map = new HashMap<>();
        map.put("type", ShopTabEnum.avatar_my == tabEnum? "2": "1");

        HttpUtils.getInstance()
                .data(model.shopMyList(map), this,
                        new OnSuccessResult<ShopMyResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<ShopMyResult> response) {
                                uc.onRefreshMyEvent.setValue(response.getData().getList());

                                //TODO 因为又retrun,所以代码放在最后
                                if (ShopTabEnum.avatar_my==tabEnum && YCollectionUtil.isNotEmpty(response.getData().getList())) {
                                    for (ShopMyEntity item: response.getData().getList()){
                                        if (YStringUtil.eq(1, item.getState())){
                                            YBusUtil.shop(YStringUtil.isNotEmpty(item.getSvgaAddress())? item.getSvgaAddress(): item.getGoodpic(), "2");
                                            return;
                                        }
                                    }
                                    YBusUtil.shop(null, "2");
                                }
                            }
                        });
    }

    /**
     * 购买
     */
    public void requestShopBuy() {
        Map<String, String> map = new HashMap<>();
        map.put("goodid", String.valueOf(currentItem.getId()));
        switch (tabEnum){
            case avatar:
                map.put("type", "2");
                break;
            case mounts:
                map.put("type", "1");
                break;
        }
        HttpUtils.getInstance()
                .data(model.shopBuy(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                YToastUtil.showShort(response.getMsg());
                            }
                        }, new OnErrorResult() {
                            @Override
                            public void onErrorResult(int code, String msg) {
                                uc.onRechargeEvent.call();
                            }
                        });
    }

    /**
     */
    public void requestShopDefault() {
        if (currentMyItem == null){
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("dressupid", currentMyItem.getId() + "");//	1坐骑 2头饰
        switch (tabEnum){
            case avatar_my:
                map.put("type", "2");
                break;
            case mounts_my:
                map.put("type", "1");
                break;
        }
        Observable observable;
        if (YStringUtil.eq(1, currentMyItem.getState())){
            observable = model.shopDefaultCancel(map);
        }else {
            observable = model.shopDefault(map);
        }
        HttpUtils.getInstance()
                .data(observable, this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                               requestShopMy();
                            }
                        });
    }

    public void onItemClick(ShopEntity entity, int position) {
        this.currentItem = entity;
        this.currentPosition = position;
        uc.onRefreshItemEvent.call();
        push();
    }

    public void onItemClick(ShopMyEntity entity, int position) {
        this.currentMyItem = entity;
        this.currentPosition = position;
        requestShopDefault();
    }

    public void push() {
        if (ShopTabEnum.avatar == tabEnum){
            YBusUtil.shop(YStringUtil.isNotEmpty(currentItem.getSvgaAddress()) ? currentItem.getSvgaAddress() : currentItem.getImgurl(), "1");
        }
    }


}
